package com.shanshan.crowd.mvc.controller;

import com.shanshan.crowd.entity.Auth;
import com.shanshan.crowd.entity.Menu;
import com.shanshan.crowd.service.api.MenuService;
import com.shanshan.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description
 * @ClassName MenuController
 * @Author 山山大魔王
 * @Date 2022/7/24 19:58
 * @Version 1.0
 */
@RestController
public class MenuController {
    @Autowired
    MenuService menuService;

    @RequestMapping("/assign/do/role/assign/menu.json")
    public ResultEntity<String> saveRoleMenuRelathinship(@RequestBody Map<String,List<Integer>> map){
        menuService.saveRoleMenuRelathinship(map);
        return ResultEntity.successWithoutData();
    }

    @RequestMapping("/menu/remove.json")
    public ResultEntity<String> removeMenu(@RequestParam("id") Integer id){
        menuService.removeMenu(id);
        return ResultEntity.successWithoutData();
    }


    @RequestMapping("/menu/update.json")
    public ResultEntity<String> updateMenu(Menu menu){
        menuService.updateMenu(menu);
        return ResultEntity.successWithoutData();
    }

    @RequestMapping("/menu/save.json")
    public ResultEntity<String> saveMenu(Menu menu){
        menuService.saveMenu(menu);

        return ResultEntity.successWithoutData();
    }

    @RequestMapping("/assgin/get/all/menu.json")
    public ResultEntity<List<Menu>> getAllMenu(){
        // 1.查询全部的Menu对象
        List<Menu> menuList = menuService.getAll();
        return ResultEntity.successWithData(menuList);
    }

    @RequestMapping("/menu/get/whole/tree.json")
    public ResultEntity<Menu> getWholeTree(){
        // 1.查询全部的Menu对象
        List<Menu> menuList = menuService.getAll();

        // 2.声明一个变量用来存储找到的根节点
        Menu root = null;

        // 3.创建Map对象用来存储id和Menu对象的对应关系便于查找父节点
        Map<Integer,Menu> menuMap = new HashMap<>();

        // 4.遍历menuList填充menuMap
        for (Menu menu : menuList) {
            Integer id = menu.getId();
            menuMap.put(id,menu);
        }

        // 5.再次遍历menuList,查找root，组装父子节点
        for (Menu menu : menuList) {
            // 6.获取pid
            Integer pid = menu.getPid();
            // 7.如果为null,则为根节点
            if (pid == null) {
                // 8.将本次循环的节点赋值给root，退出本次循环
                root = menu;
                continue;
            }
            // 9.不为null，获取父节点
            Menu maybeFather = menuMap.get(pid);
            // // 11.将子节点存入父节点的children
            List<Menu> children = maybeFather.getChildren();
            children.add(menu);

        }

        // 返回整颗树
        return ResultEntity.successWithData(root);
    }
    public ResultEntity<Menu> getWholeTreeOld(){

        // 1.查询全部的Menu对象
        List<Menu> menuList = menuService.getAll();

        // 2.声明一个变量用来存储找到的根节点
        Menu root = null;

        // 3.遍历menuList
        for (Menu menu : menuList) {
            // 4.获取当前menu对象的pid属性值
            Integer pid = menu.getPid();

            // 5.检查pid是否为null
            if (pid == null){
                // 6.把正在遍历的这个menu赋值给root
                root = menu;
                continue ;
            }

            // 8.如果pid不为null，说明当前节点有父节点, 找到父节点进行组装，建立父子关系
            for (Menu maybeFather : menuList) {
                // 9.获取maybeFather的id值
                Integer id = maybeFather.getId();
                // 10.将子节点的pid和id比较
                if (Objects.equals(pid, id)){
                    // 11.将子节点存入父节点的children
                    List<Menu> children = maybeFather.getChildren();
                    children.add(menu);
                    // 12.找到即可停止运行循环
                    break;
                }
            }
        }

        // 返回根节点
        return ResultEntity.successWithData(root);
    }



}
